package com.codes.controller;

import com.codes.core.common.enumtype.wordtpl.CaseTypeEnum;
import com.codes.core.common.enumtype.wordtpl.TplPersonTypeEnum;
import com.codes.core.common.enumtype.wordtpl.TplTableMappingEnum;
import com.codes.core.common.result.Result;
import com.codes.core.utils.word.PoiTlUtils;
import com.codes.core.utils.word.WordUtils;
import com.codes.service.CaseDocumentSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description 
 * @Date 2020/7/6 14:35
 * @Created  peanut
 */

@Slf4j
@RestController
@Api(tags = "案件文书 接口")
@RequestMapping("/document")
public class CaseDocumentCtr  {

    @Autowired
    private CaseDocumentSV caseDocumentSV;

    @ApiOperation("getlist")
    @GetMapping("/list")
    public Result getList(@RequestParam Map<String,Object> map){

        System.out.println(2222222);
        System.out.println(333);
        System.out.println(444);
        map.put("caseId",1);

//        try {
//            File f = PoiTlUtils.getWordDoc("","",null);
//            System.out.println(f.getName());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        List<Map<String,Object>> list = caseDocumentSV.getList(map);
        if (list.size() > 0) return Result.success(list);

        return Result.failed("获取list异常");
    }

    @ApiOperation("save")
    @PostMapping("/save")
    public Result saveDoc(@RequestParam Map<String,Object> map){

        int re = caseDocumentSV.insertCaseDoc(1,1, CaseTypeEnum.zaizhiliang
                , TplTableMappingEnum.zhandianxinxi, TplPersonTypeEnum.geren);
        if (re > 0) return Result.success("保存ok");

        return Result.failed("保存shib");
    }

    @ApiOperation("获取文书根据ID")
    @GetMapping("/get-word")
    public void getWord(@RequestParam Map<String,Object> map, HttpServletRequest request, HttpServletResponse response) throws IOException {

        Integer caseDocumentId = 5;


        File file = caseDocumentSV.getDoc(caseDocumentId);
        WordUtils.fileToResponse(response,file);


    }


}